package com.zl.filter;

import com.zl.domain.User;
import com.zl.service.UserService;
import com.zl.util.Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * Created by zhLEi on 2016/4/24.
 */
public class AutoLoginInterceptor implements HandlerInterceptor {
    @Autowired
    private UserService userService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
        String uri = request.getRequestURI(); // /.*\.(?:(?!(css|js|png)))/
        if (uri.equals("/") || !uri.matches(".*?\\/(?:.*?\\.(?!css$|js$|png$|html$)|[^\\.]*$)")) {
            return true;
        }
        if (request.getSession().getAttribute("user") == null) {
            if (uri.contains("login")) return true;
            Cookie cookies[] = request.getCookies();
            String token = null, splitStr = Util.md5(",;."), solt = Util.md5("cnmlgb");
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    if (cookie.getName().equals("atlg")) {
                        token = cookie.getValue();
                    }
                }
                String username = null, strs[] = new String[2];
                if (!("").equals(token) && token != null) {
                    strs = token.split(splitStr);
                    username = strs[0];
                }
                if (username != null && !("").equals(username) && solt.equals(strs[1])) {
                    User user = new User();
                    user.setUsername(username);
                    user.setAutoLogin(1);
                    Map<String, Object> userMap = userService.findUser(user);
                    if (userMap == null || userMap.size() == 0)
                        response.sendRedirect(request.getContextPath() + "/login");
                    else {
                        userMap.put("privileges", userService.findPrivileges(userMap.get("rid") + ""));
                        request.getSession().setAttribute("user", userMap);
                        response.sendRedirect(request.getContextPath() + "/toIndex");
                    }
                    return true;
                } else {
                    if (uri.contains("login") || uri.contains("logout")) {
                        return true;
                    }
                    request.getRequestDispatcher("/login").forward(request, response);
                    return false;
                }
            } else {
                request.getRequestDispatcher("/login").forward(request, response);
                return false;
            }
        } else {
            if (uri.contains("login"))
                response.sendRedirect(request.getContextPath() + "/toIndex");
            return true;
        }
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o, ModelAndView
            modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse
            httpServletResponse, Object o, Exception e) throws Exception {

    }
}
